package com.snapp.web.actions;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import com.snapp.util.object.model.ObjectModel;
import com.snapp.web.SNAppAction;
import com.snapp.web.agents.SNAppAgent;

public class Save extends SNAppAction {
	private HashMap<String,Object> parameterMap = null;
	private ObjectModel object = null;
	
	public String execute(HttpServletRequest req) {
		setSNAppGlobalId(req.getParameter("SNAppGlobalId"));
		if (getSNAppGlobalId() == null) {
			setResultCode(-1);
			setResultMessage("Missing \"SNAppGlobalId\" parameter.  Not sure which object to save.");
			return super.execute(req);
		}
		
		try {
			if (parameterMap == null) parameterMap = consolidateWriteMap(req);
			
			SNAppAgent agent = new SNAppAgent();
			object = agent.save(getSNAppGlobalId(), parameterMap);
			
			setResultCode(1);
			setResultMessage("Object saved successfully.");

			setBodyElement(object.getBean().toXML());
	
		} catch(Exception e) {
			log("Error parsing dbo bean to xml.", e);
		}
		return super.execute(req);
	}

	public HashMap<String, Object> getParameterMap() {
		return parameterMap;
	}

	public void setParameterMap(HashMap<String, Object> parameterMap) {
		this.parameterMap = parameterMap;
	}

	public ObjectModel getObject() {
		return object;
	}

	public void setObject(ObjectModel object) {
		this.object = object;
	}

}

